// Phong-Blinn shading model fragment shader

uniform sampler2D baseMap;
uniform sampler2D specularMap;

varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
varying float Distance;

vec4 fvGetPhongBlinnIllumination(vec2 texCoord, bool notSelfShadowed)
{
   vec3 fvLightDirection = normalize( LightDirection );
   vec3 fvViewDirection = normalize( ViewDirection );
   vec3 fvHalf = normalize( fvLightDirection + fvViewDirection );
   
   vec3 fvNormal = FS_GET_NORMAL(texCoord);
   
   float fNDotL = max ( 0.0, dot( fvNormal, fvLightDirection ) );
   float fNDotH = max ( 0.0, dot( fvNormal, fvHalf ) );

   vec4 fvTotalDiffuse = gl_LightSource[0].diffuse * fNDotL;
   vec4 fvTotalSpecular = gl_LightSource[0].specular * ( pow( fNDotH, gl_FrontMaterial.shininess ) ) * texture2D( specularMap, texCoord );
   
   float fAttenuation = 1.0 / (gl_LightSource[0].constantAttenuation +
                               gl_LightSource[0].linearAttenuation * Distance +
                               gl_LightSource[0].quadraticAttenuation * Distance * Distance);
   
   if ( !notSelfShadowed && fNDotL != 0.0 ) fvTotalDiffuse = fvShadowColor * (1.0 - fNDotL);
      
   return (fvTotalDiffuse + fvTotalSpecular) * fAttenuation;
}
